package com.taskmanagement.controller;

import com.taskmanagement.entity.Step;
import com.taskmanagement.service.AIService;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Slf4j
@RestController
@RequestMapping("/api/ai")
@RequiredArgsConstructor
public class AIController {

    private final AIService aiService;

    @PostMapping("/generate-steps")
    public ResponseEntity<List<Step>> generateSteps(@Valid @RequestBody GenerateStepsRequest request) {
        log.info("收到生成步骤链请求 - 任务: {}", request.getTaskName());

        List<Step> steps = aiService.generateStepChain(request.getTaskName(), request.getRequirements());

        return ResponseEntity.ok(steps);
    }

    @PostMapping("/generate-steps-async")
    public CompletableFuture<ResponseEntity<List<Step>>> generateStepsAsync(@Valid @RequestBody GenerateStepsRequest request) {
        log.info("收到异步生成步骤链请求 - 任务: {}", request.getTaskName());

        return aiService.generateStepChainAsync(request.getTaskName(), request.getRequirements())
                .thenApply(ResponseEntity::ok);
    }

    @PostMapping("/optimize-steps")
    public ResponseEntity<List<Step>> optimizeSteps(@Valid @RequestBody OptimizeStepsRequest request) {
        log.info("收到优化步骤链请求 - 步骤数: {}", request.getExistingSteps().size());

        List<Step> optimizedSteps = aiService.optimizeStepChain(request.getExistingSteps(), request.getFeedback());

        return ResponseEntity.ok(optimizedSteps);
    }

    @PostMapping("/adjust-steps")
    public ResponseEntity<List<Step>> adjustSteps(@Valid @RequestBody AdjustStepsRequest request) {
        log.info("收到调整步骤链请求 - 步骤数: {}", request.getCurrentSteps().size());

        List<Step> adjustedSteps = aiService.adjustStepChain(
                request.getCurrentSteps(),
                request.getProgressUpdate(),
                request.getChallenges()
        );

        return ResponseEntity.ok(adjustedSteps);
    }

    // DTO类
    @Setter
    @Getter
    public static class GenerateStepsRequest {
        // getters and setters
        @NotBlank(message = "任务名称不能为空")
        @Size(max = 100, message = "任务名称长度不能超过100个字符")
        private String taskName;

        @NotBlank(message = "需求描述不能为空")
        @Size(max = 2000, message = "需求描述长度不能超过2000个字符")
        private String requirements;

    }

    @Setter
    @Getter
    public static class OptimizeStepsRequest {
        // getters and setters
        @jakarta.validation.Valid
        private List<Step> existingSteps;

        @NotBlank(message = "反馈内容不能为空")
        @Size(max = 1000, message = "反馈内容长度不能超过1000个字符")
        private String feedback;

    }

    @Setter
    @Getter
    public static class AdjustStepsRequest {
        // getters and setters
        @jakarta.validation.Valid
        private List<Step> currentSteps;

        @Size(max = 1000, message = "进度更新内容长度不能超过1000个字符")
        private String progressUpdate;

        @Size(max = 1000, message = "挑战描述长度不能超过1000个字符")
        private String challenges;

    }
}
